﻿using UnityEngine;
using System.Collections;
using DICE_GS;

public class UnitStateChangeResultManager : MonoBehaviour {
	public UITexture beforeUnitImage;
	public UILabel beforeUnitName;
	public UILabel beforeUnitLevel;
	public UILabel beforeUnitHP;
	public UILabel beforeUnitAttack;
	public UILabel beforeUnitDefense;
	public UILabel beforeUnitMove;
	
	public UITexture afterUnitImage;
	public UILabel afterUnitName;
	public UILabel afterUnitLevel;
	public UILabel afterUnitHP;
	public UILabel afterUnitAttack;
	public UILabel afterUnitDefense;
	public UILabel afterUnitMove;
	
	static public bool isLevelUp;
	static public int beforeUnitDBId=0;
	static public int beforeUnitExp=0;
	static public int afterUnitDbId=0;
	
	static public int newUnitClassId = 0;
    static public int prevUnitLevel = 0;
	
	// Use this for initialization
	void Start () {
		int level=0;
		UnitInfo currentUnitInfo = MyInfo.Instance.GetUnitInfoById(MyInfo.Instance.LastSelectedUnitDBID);
		if( currentUnitInfo != null )
		{
			level = currentUnitInfo.UnitLevel;
		}
		CUnitData baseUnitData = CUnitDataManager.Instance.GetUnitData(MyInfo.Instance.LastSelectedUnitID);
        CUnitAbilityItem plusUnitData = CUnitAbilityDataManager.Instance.GetUnitAbilityData(level + "_" + baseUnitData.unit_class);
        CUnitAbilityItem prevplusUnitData = CUnitAbilityDataManager.Instance.GetUnitAbilityData(prevUnitLevel + "_" + baseUnitData.unit_class);
		if( MyInfo.Instance.LastSelectedUnitID != 0 )
		{
			Texture2D tex = Resources.Load(baseUnitData.name) as Texture2D;
			if( tex != null )
				beforeUnitImage.mainTexture = tex;
			else
				beforeUnitImage.mainTexture = Resources.Load("N_ea_01") as Texture2D;
//			beforeUnitImage.spriteName = "card_"+baseUnitData.name;
			beforeUnitName.text = currentUnitInfo.name;
			if( currentUnitInfo != null )
                beforeUnitLevel.text = prevUnitLevel.ToString();
			beforeUnitHP.text = (baseUnitData.max_hp + prevplusUnitData.max_hp).ToString();
            beforeUnitAttack.text = (baseUnitData.attack_base + prevplusUnitData.attack).ToString();
            beforeUnitDefense.text = (baseUnitData.defense_base + prevplusUnitData.defense).ToString();
			beforeUnitMove.text = (baseUnitData.move_base+prevplusUnitData.move).ToString();

			if( isLevelUp )
			{
				Texture2D tex2 = Resources.Load(baseUnitData.name) as Texture2D;
				if( tex2 != null )
					afterUnitImage.mainTexture = tex2;
				else
					afterUnitImage.mainTexture = Resources.Load("N_ea_01") as Texture2D;
//				afterUnitImage.spriteName = "card_"+baseUnitData.name;
				afterUnitName.text = currentUnitInfo.name;
				if( currentUnitInfo != null )
					afterUnitLevel.text = currentUnitInfo.UnitLevel.ToString();
				afterUnitHP.text = (baseUnitData.max_hp + plusUnitData.max_hp).ToString();
                afterUnitAttack.text = (baseUnitData.attack_base + plusUnitData.attack).ToString();
                afterUnitDefense.text = (baseUnitData.defense_base + plusUnitData.defense).ToString();
                afterUnitMove.text = (baseUnitData.move_base + plusUnitData.move).ToString();	
				
				LerpValue(afterUnitLevel, prevUnitLevel, level);
                LerpValue(afterUnitHP, (baseUnitData.max_hp + prevplusUnitData.max_hp), (baseUnitData.max_hp + plusUnitData.max_hp));
                LerpValue(afterUnitAttack, (baseUnitData.attack_base + prevplusUnitData.attack), (baseUnitData.attack_base + plusUnitData.attack));
                LerpValue(afterUnitDefense, (baseUnitData.defense_base + prevplusUnitData.defense), (baseUnitData.defense_base + plusUnitData.defense));
                LerpValue(afterUnitMove, (baseUnitData.move_base + prevplusUnitData.move), (baseUnitData.move_base + plusUnitData.move));
			}
			else
			{
				baseUnitData = CUnitDataManager.Instance.GetUnitData(newUnitClassId);
				Texture2D tex2 = Resources.Load(baseUnitData.name) as Texture2D;
				if( tex2 != null )
					afterUnitImage.mainTexture = tex2;
				else
					afterUnitImage.mainTexture = Resources.Load("N_ea_01") as Texture2D;
//				afterUnitImage.spriteName = "card_"+baseUnitData.name;
				afterUnitName.text = currentUnitInfo.name;
				afterUnitLevel.text = "1";
				afterUnitHP.text = baseUnitData.max_hp.ToString();
				afterUnitAttack.text = baseUnitData.attack_base.ToString();
				afterUnitDefense.text = baseUnitData.defense_base.ToString();
				afterUnitMove.text = baseUnitData.move_base.ToString();
			}
		}
	}
	
	// Update is called once per frame
	void Update () {
		
	}
	
	void LerpValue(UILabel obj, int max)
	{
		LerpValue(obj, 0, max);
	}
	
	void LerpValue(UILabel obj, int min, int max)
	{
		StartCoroutine(Co_LerpValue(obj, min, max));
	}
	
	IEnumerator Co_LerpValue(UILabel obj, int min, int max)
	{
		yield return null;
		
		float step = 1/3.0f;
		float progress = 0f;
		
		while( progress < 1f )
		{
			SetNumber(obj, 2f * progress, min, max);
			progress += step*Time.deltaTime;
			yield return null;
		}
	}
	
	void SetNumber(UILabel obj, float f, int min, int max)
	{
		int num = Mathf.FloorToInt( Mathf.Lerp((float)min, (float)max, f) );
		obj.text = num.ToString();
	}
}
